﻿package Cain.pcxbook
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.utils.getDefinitionByName;
	import flash.display.DisplayObject;
	import Cain.mc_cesh.Cesher;
	/**
	 * ...//載入圖片 並將他放到適合大小 v1.3
	 * @author Cain in Liontreval 2009
	 * 
	 * 宣告====================================
	 * new PcxAutoSize() extends Sprite
	 * 
	 * 屬性====================================
	 * W :Number 寬
	 * H :Number 高
	 * Full :Boolean 滿版
	 * isload:Boolean 已有圖了
	 * 
	 * 方法====================================
	 * setup(  寬:Number, 高:Number)
	 * load(圖片路徑:Steite,是否滿版:Boolen = true , 元件庫中loading的影片Class名:String = null)
	 * 
	 * 事件====================================
	 * Event.COMPLETE  圖載完
	 */
	public class  PcxAutoSize extends Sprite
	{
		private var pcx_url:String;
		private var ww:Number;
		private var hh:Number;
		private var loadingClassName:String;
		private var mk:Sprite;
		private var loader:Loader;
		private var full:Boolean;
		private var is_load:Boolean;
		public function PcxAutoSize() 
		{
			init();
		}
		
		public function get W():Number 
		{
			return ww;
		}
		public function set W(_w:Number):void 
		{
			mk.width = _w;
			ww = _w;
			loader.scaleX = 1;
			loader.scaleY = 1;
			//setSize();
		}
		public function get H():Number 
		{
			return hh;
		}
		public function set H(_h:Number):void 
		{
			mk.height = _h;
			hh = _h;
			loader.scaleX = 1;
			loader.scaleY = 1;
			//setSize();
		}
		public function get Full():Boolean 
		{
			return full;
		}
		public function set Full(_full:Boolean):void
		{
			loader.scaleX = 1;
			loader.scaleY = 1;
			full = _full;
			//setSize();
		}
		private function init():void
		{
			
		}
		public function setup(_ww:Number,_hh:Number):void 
		{
			
			ww = _ww;
			hh = _hh;
			MK_setup();//遮罩初始化
			
		}
		//遮罩初始化
		private function MK_setup():void
		{
			mk = new Sprite();
			mk.graphics.beginFill(0, 0);
			mk.graphics.drawRect(0, 0, ww, hh);
			mk.graphics.endFill();
			mask = mk;
			addChild(mk);
			
			//loader.mask = mk; 4/27改
			
		}
		public function load(_pcx_url:String,_full:Boolean = true,_loadingClassName:String = null):void 
		{
			loader = new Loader();
			addChild(loader);
			try 
			{
				removeChild(getChildByName("loadingMC"));
				loader.close();
				
			}catch (err:Error)
			{
				
			}
			try 
			{
				loader.unload();
			}catch (err:Error){}
			pcx_url = _pcx_url;
			full = _full;
			loadingClassName = _loadingClassName;
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded);
			loading_setup();//設定loading動畫
			
			loader.load(new URLRequest(pcx_url));
		}
		//停止loading
		public function close():void 
		{
			loader.close();
			if (loadingClassName!=null) 
			{
				removeChild(getChildByName("loadingMC"));
			}
		}
		//設定loading動畫
		private function loading_setup():void
		{
			if (loadingClassName!=null) 
			{//如果loadingClassName有東西
				try 
				{
					var new_class:Class = getDefinitionByName(loadingClassName) as Class;
					var kk:DisplayObject = new new_class();
					kk.x = ww / 2;
					kk.y = hh / 2;
					kk.name = "loadingMC";
					addChild(kk);
				}catch (e:Error)
				{
					trace("元件庫中沒有" + loadingClassName);
				}
				
			}
		}
		//圖形載完
		private function loaded(e:Event):void 
		{
			var _bt:BitmapData = new BitmapData(loader.width, loader.height);
			var btda:Bitmap = new Bitmap();
			btda = new Bitmap(Bitmap(loader.content).bitmapData.clone());
			var sp:Sprite = new Sprite();
			sp.addChild(btda);
			btda.x = -btda.width / 2;
			btda.y = -btda.height / 2;
			/*try 
			{
				Bitmap(loader.content).smoothing = true;
			}catch (e:Error)
			{
				trace("內容不是點陣物件");
			}*/
			setSize(sp);//設定圖大小
			
			if (loadingClassName!=null) 
			{
				removeChild(getChildByName("loadingMC"));
			}
			is_load = true;
			
			//=======================
			var con:Cesher = new Cesher(this, new Rectangle(0, 0, ww, hh));
			var bit = new Bitmap(con.bitmapData());
			try 
			{
				removeChild(loader);
			}catch (err:Error)
			{
				
			}
			
			addChild(bit);
			dispatchEvent(new Event(Event.COMPLETE));
		}
		/**
		 * 已經有圖了
		 * @return true 代表已載完一次了
		 */
		public function get isload():Boolean 
		{
			return is_load;
		}
		//設定圖大小
		private function setSize(sp:Sprite):void
		{
			var sc_w:Number = ww/sp.width;
			var sc_h:Number = hh / sp.height;
			
			if (full) 
			{//滿版
				if (sc_w>=sc_h) 
				{
					sp.scaleX = sc_w;
					sp.scaleY = sc_w;
				}else 
				{
					sp.scaleX = sc_h;
					sp.scaleY = sc_h;
				}
			}else 
			{//顯示整個
				if (sc_w<=sc_h) 
				{
					sp.scaleX = sc_w;
					sp.scaleY = sc_w;
				}else 
				{
					sp.scaleX = sc_h;
					sp.scaleY = sc_h;
				}
			}
			set_md(sp);//置中
		}
		//置中
		private function set_md(sp:Sprite):void
		{
			var md_x:Number = mk.width / 2;
			var md_y:Number = mk.height / 2;
			sp.x = md_x;
			sp.y = md_y;
			
			
		}
		
		
	}
	
}